package com.qin.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.blog.domain.TUsers;
import com.qin.blog.dto.UserDto;
import com.qin.blog.service.TUsersService;
import com.qin.blog.mapper.TUsersMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 覃剑
* @description 针对表【t_users】的数据库操作Service实现
* @createDate 2022-10-25 09:40:25
*/
@Service
public class TUsersServiceImpl extends ServiceImpl<TUsersMapper, TUsers>
    implements TUsersService{
    
    @Autowired
    private TUsersMapper usersMapper;

    @Override
    public TUsers insertUser(UserDto userDto) {
        TUsers users = new TUsers();
        BeanUtils.copyProperties(userDto,users);
        //条件构造器
        QueryWrapper<TUsers> queryWrapper = new QueryWrapper<>();
        //查询数据库是否同名
        queryWrapper.eq("username",userDto.getName());
        List<TUsers> list = usersMapper.selectList(queryWrapper);
        if(list.size()>0){
            System.out.println(list.size());
            return null;
        }
        else {
            users.setUsername(userDto.getName());
            users.setPassword(userDto.getPassword());
            Integer result = usersMapper.insert(users);
            return users;
        }
    }

    @Override
    public TUsers selectUser(Long id) {
        System.out.println("我来到这了");
        TUsers users = usersMapper.selectById(id);
        System.out.println("又到这了");
        System.out.println(users);
        return users;
    }
}




